<template>
  <div class="login-container">
    <div class="form-box">
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-xingmingyonghumingnicheng1"
            v-model="ruleForm.username"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-mima"
            v-model="ruleForm.password"
            show-password
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btn-box">
          <el-button type="primary" size="small" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button type="info" size="small" @click="resetForm('ruleForm')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
           
<script>
import { login_api } from "@/api";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "888",
      },
      rules: {
        uname: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      // this.$refs[formName].validate((valid) => {     //点击登录时候做第二次表单验证   ,
      // // console.log(valid);    ui自带的验证规则   显示true 和false
      //   if (valid) {
      //     //成功的代码   //异步函数
      //     this.$axios.post('/login',this.ruleForm).then((res)=>{
      //       console.log(res);
      //       //如果状态码等于200时,
      //       if(res.data.meta.status===200){
      //           this.$message.success('恭喜你，登录成功')
      //         this.$router.push('/home')
      //       }else{   //如果状态码不等于200
      //         this.$message.error(res.data.meta.msg)
      //       }
      //     })
      //   } else {

      //      this.$message.error('表提交错误')
      //     return false;
      //   }
      // });
      //   // console.log(this);    //点击的时候可以在这个组件的vue上面查到$axios,
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //成功的代码   //异步函数
          var res = await login_api(this.ruleForm);  //this.ruleForm实参
          // console.log(res);
          //如果状态码等于200时,
          if (res.data.meta.status === 200) {
            this.$message.success("恭喜你，登录成功");
            this.$router.push("/home");
              /* 将token存入本地缓存 */
            window.sessionStorage.setItem('token',res.data.data.token)
          } else {
            //如果状态码不等于200
            this.$message.error(res.data.meta.msg);
          }
        } else {
          this.$message.error("表提交错误");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped lang="less">
.login-container {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
  .form-box {
    width: 450px;
    height: 300px;
    background-color: white;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .logo {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      background-color: #eeeeee;
      border: 10px solid white;
      // padding: 5px;
      position: absolute;
      left: 50%;
      top: -50px;
      transform: translate(-50%, 0);
      overflow: hidden;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .el-form {
      //在页面解构中找到这个类名  这个是ui自动给加上的
      margin-top: 100px;
      padding: 0 25px;
      .btn-box {
        text-align: right;
      }
    }
  }
}
</style>